﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace FrameManager
{
    [XmlInclude(typeof(ProductSetting))]
    public class ProductSetting
    {
        [Browsable(false)]
        public string ProjectName
        {
            get;
            set;
        }
        [Browsable(false)]
        public int Total
        {
            get;
            set;
        }
        [Browsable(false)]
        public int Input
        {
            get;
            set;
        }
        [Browsable(false)]
        public int Pass
        {
            get;
            set;
        }
        [Browsable(false)]
        public int Fail
        {
            get;
            set;
        }
        [Category("自动排胶设定")]
        public double 自动排胶间隔时间
        {
            get;
            set;
        }
        [Category("自动排胶设定")]
        public double 自动排胶开胶时间
        {
            get;
            set;
        }
        [Category("自动排胶设定"), XmlIgnore]
        public double 自动排胶关胶时间
        {
            get;
            set;
        }
        [Category("禁止设定"),XmlIgnore]
        public bool 禁止自动排胶
        {
            get;
            set;
        }
        [Category("设备设定")]
        public bool 流水线反反方向
        {
            get;
            set;
        }
        [Category("禁止设定")]
        public bool 禁止出胶
        {
            get;
            set;
        }
        [Category("追踪区间大小")]
        public double 追踪区间大小
        {
            get;
            set;
        }
        [Category("图像设定")]
        public bool 保存NG图片
        {
            get;
            set;
        }
        [Category("图像设定")]
        public bool 保存OK图片
        {
            get;
            set;
        }
        [Category("图像设定")]
        public string 保存图片路径
        {
            get;
            set;
        }
        public ProductSetting()
        {
            保存图片路径 = @"D:\HistoryImage\";
        }
        public static ProductSetting Load()
        {
            ProductSetting pDoc;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ProductSetting));
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/ProductSetting" + ".xml");
                pDoc = (ProductSetting)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
            }
            catch (IOException)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new ProductSetting();

            }
            catch (Exception)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new ProductSetting();


            }
            return pDoc;
        }
        public bool Save()
        {
            if (!Directory.Exists(Application.StartupPath + @"/MuduleParameter/"))
            {
                Directory.CreateDirectory(Application.StartupPath + @"/MuduleParameter/");
            }
            FileStream fsWriter = new FileStream(Application.StartupPath + @"/MuduleParameter/ProductSetting" + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ProductSetting));
            xmlSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();

            return true;
        }
    }
}
